<?php
namespace app\service;
use think\facade\Db;

class adminService{
    //登录检测
    public function login($data){
		return Db::name('auth_users')
				->where('name',$data['name'])
				->where('password',md5($data['password']))
				->find();
	}
    //查询管理员数据
    public function adminList(){
        return Db::name('admin')->where("usertype","neq","0")->order('id asc')->select();
    }
    //查询管理员条数
    public function adminNumber(){
        return Db::name('admin')->select();
    }
    //判断管理员账号是否重复
    public function adminName($name){
        return Db::name('admin')->where('name',$name)->find();
    }
    //添加管理员
    public function adminAdd($data){
        return Db::name('admin')->insertGetId($data);
    }
    //查询指定ID管理员的数据
    public function adminIdList($id){
        return Db::name('admin')->where('id',$id)->find();
    }
    //查询角色信息
    public function authGroup(){
        return Db::name('auth_group')->order('id asc')->select();
    }
    //添加用户角色信息
    public function adminGroup($juese){
        return Db::name('auth_group_access')->insert($juese);
    }
    //
    public function adminIdGroupId($id){
        return Db::name('auth_group_access')->where('uid',$id)->find();
    }
    //修改数据
    public function adminUpdate($data){
        return Db::name('admin')->update($data);
    }
    public function adminGroupUpdate($uid,$juese){
        return Db::name('auth_group_access')->where('uid',$uid)->update($juese);
    }
    //查询权限分类数据
    public function astrictList(){
        $data=Db::name('auth_rule')->order('id asc')->select();
        return $this->classSort($data);
    }
    public function classSort($data,$pid=0,$level=0){
        static $arr=array();
        foreach ($data as $k => $v){
            if($v['pid'] == $pid){
                $v['level'] =$level;
                $arr[]=$v;
                $this->classSort($data,$v['id'],$level+1);
            }
        }
       return $arr;
    }
    //判断权限名称是否重复
    public function astrictName($title){
        return Db::name('auth_rule')->where('title',$title)->find();
    }
    //添加权限分类
    public function astrictAdd($data){
        return Db::name('auth_rule')->insert($data);
    }
    //查询指定ID权限的数据
    public function astrictIdList($id){
        return Db::name('auth_rule')->where('id',$id)->find();
    }
    //修改权限属性
    public function astrictUpdate($data){
        return Db::name('auth_rule')->update($data);
    }
    //查询指定ID权限下是否有权限
    public function astrictZidList($id){
        return Db::name('auth_rule')->where('pid',$id)->find();
    }
    //查询角色数据
    public function roleList(){
        return Db::name('auth_group')->order('id asc')->select();
    }
    //角色中查询权限数据
    public function roleAst(){
        $data=Db::name('auth_rule')->order('id asc')->select();
        return $this->roleClass($data);
    }
    public function roleClass($data,$pid=0,$level=0){
        static $arr=array();
        foreach ($data as $k => $v){
            if ($v['pid'] == $pid) {
                if ($pid == 0) {
                    $arr[$v['id']] = $v;
                }else{
                    $arr[$v['pid']]['child'][] = $v;
                }
                $this->roleClass($data, $v['id']);
            }
        }
        return $arr;
    }
    //查询角色名称是否重复
    public function roleName($title){
        return Db::name('auth_group')->where('title',$title)->find();
    }
    //添加角色
    public function roleAdd($data){
        return Db::name('auth_group')->insert($data);
    }
    //查询指定ID角色的数据
    public function roleIdList($id){
        return Db::name('auth_group')->where('id',$id)->find();
    }
    //修改角色属性
    public function roleUpdate($data){
        return Db::name('auth_group')->update($data);
    }
    public function propertyInfo(){
		return Db::name('property')->select();
	}
	public function communityInfo(){
		return Db::name('community')->select();
    }
    public function serviceInfo(){
		return Db::name('service')->select();
    }
    public function propertyNameId($name){
        return Db::name('property')->where('name',$name)->value('id');
    }
    public function communityNameId($name){
        return Db::name('community')->where('community_name',$name)->value('id');
    }
    public function serviceNameId($name){
        return Db::name('service')->where('name',$name)->value('id');
    }
}